package com.xbongbong.saas.service.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.inviteUser.pojo.dto.InviteUserDTO;
import com.xbongbong.pro.inviteUser.pojo.vo.InviteUserVO;
import com.xbongbong.pro.message.constant.InviteStaffConstant;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.PushRefTypePojo;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.service.MessageService;
import com.xbongbong.saas.service.InviteUserService;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 发票业务实现
 *
 * @author chy
 * @version v1.0
 * @date 2019/1/22 15:10
 * @since v1.0
 */
@Service("inviteUserService")
public class InviteUserServiceImpl implements InviteUserService {

    private static final Logger LOG = LoggerFactory.getLogger(InviteUserServiceImpl.class);

    @Resource
    private UserModel userModel;
    @Resource
    private MessageService messageService;

    @Override
    public InviteUserVO inviteUser(InviteUserDTO inviteUserDTO) throws XbbException {
        InviteUserVO inviteUserVO = new InviteUserVO();
        List<String> userIds = inviteUserDTO.getUserIds();
        String corpid = inviteUserDTO.getCorpid();
        String title = I18nMessageUtil.getMessage(InviteStaffConstant.INVITE_STAFF_TITLE);

        String userName = "";

        String userId = inviteUserDTO.getUserId();
        UserEntity userEntity = userModel.getByKey(userId, corpid);
        if (userEntity != null) {
            userName = userEntity.getName();
        }

        String content = String.format(I18nMessageUtil.getMessage(InviteStaffConstant.INVITE_STAFF), userName);
        List<Long> refIds = Collections.singletonList(-1L);
        PushRefTypePojo.Options options = new PushRefTypePojo.Options();
        MessageRabbitMqDTO messageRabbitMqDTO = new MessageRabbitMqDTO(corpid, userIds, refIds, 0, null, title, content, null, options);

        messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.INVITE_STAFF_PUSH);
        return inviteUserVO;
    }
}
